class DocumentsController < ApplicationController
  before_filter :find_section, :only=>['index', 'new' , 'create']
  load_and_authorize_resource :nested=>Section
  caches_page :show
  cache_sweeper :document_sweeper, :only => [ :update, :destroy]

  # GET /documents
  # GET /documents.xml
  def index
    @documents = @section.documents.paginate(:page=>params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @documents }
    end
  end

  # GET /documents/search
  # GET /documents/search.xml
  def search
    query = Document.make_better(params[:query]||'')
    page = (params[:page]||1).to_i-1
    if !params[:search_range] || params[:search_range] == "all"
      # Search all documents
      @search_results = Document.search(query,
        :offset=>page * SolrPaginationExtender::ResultsPerPage,
        :limit=>SolrPaginationExtender::ResultsPerPage)
    else
      # Search selected range only
      selected_model, selected_id = params[:search_range].split('_')
      selected_model = selected_model.camelize.constantize
      selected_id = selected_id.to_i
      @search_results = selected_model.find(selected_id).search(query,
        :offset=>page * SolrPaginationExtender::ResultsPerPage,
        :limit=>SolrPaginationExtender::ResultsPerPage)

    end
    @search_results.extend(SolrPaginationExtender)
    @search_highlights = Document.highlight(@search_results.docs, params[:query])

    respond_to do |format|
      format.html
      format.xml  { render :xml => @search_results.docs }
    end
  end

  # GET /documents/1
  # GET /documents/1.xml
  def show
    @document = Document.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/new
  # GET /documents/new.xml
  def new
    @document = @section.documents.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/1/edit
  def edit
    @document = Document.find(params[:id])
  end

  # POST /documents
  # POST /documents.xml
  def create
    @document = @section.documents.new(params[:document])

    respond_to do |format|
      if @document.save
        flash[:notice] = 'Document was successfully created.'
        format.html { redirect_to(collection_section_document_url(@collection, @section, @document)) }
        format.xml  { render :xml => @document, :status => :created, :location => @document }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /documents/1
  # PUT /documents/1.xml
  def update
    @document = Document.find(params[:id])

    respond_to do |format|
      if @document.update_attributes(params[:document])
        flash[:notice] = 'Document was successfully updated.'
        format.html { redirect_to document_path(@document) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.xml
  def destroy
    @document = Document.find(params[:id])
    @document.destroy

    respond_to do |format|
      format.html { redirect_to(collection_section_documents_url(@collection, @section)) }
      format.xml  { head :ok }
    end
  end
protected
  def find_section
    @section = Section.find(params[:section_id])
  end
end

